<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>短信验证码</title>
</head>

<body>
  <input type="text" />
  <button type="button">发送验证码</button>

  <script>
    let time = 20
    const btn = document.querySelector('button')
    btn.onclick = function () {
      const timer = setInterval(function () {
        if (time < 10) {
          time = '0' + time
        }
        btn.innerHTML = `剩余${time}秒`
        time--
        if (time < 0) {
          clearInterval(timer)
          btn.innerHTML = `发送验证码`
          btn.disabled = false
        }
      }, 1000)

      this.disabled = true
    }
  </script>
</body>

</html>